// import PIXI from './PIXI.js';
import Component from './Component.js';
import Note from './Note.js';

let app =window.app;

export default class Music extends Component{
  
  constructor({bpm, duration, src, width, height, beatHeight, onMiss, onEnd}){
    super();
    this.speed = 1000;
    this.trackIndex = null;

    const sprite =  new PIXI.Container();
    sprite.width = width;
    sprite.height = height;
    sprite.x = 0;
    sprite.y = 0;
    this.sprite = sprite;
    window.app.stage.addChild(this.sprite);
    this.width = width;
    this.height = height;
    this.bpm = 240; //节奏
    this.duration = 30000; //持续时间
    this.trackNum = 4; //赛道数
    this.src = src;
    this.size = Math.round(this.bpm * this.duration/60000);  

    this.list = [];
    this.playList = [];
    this.playIndex = 0;
    this.startTime = null;
		this.isEnd = false;

    this.beatHeight = beatHeight;
    this.onMiss = onMiss;
    this.onEnd = onEnd;

     this.vs = width/375;
    console.log('initNote',' bpm：',this.bpm, ' 持续时间：',this.duration,' 掉落总数：',this.size)
  }
  getTrackIndex(){
    //连续两个不重复
    const arr = Array.from(Array(this.trackNum), (v,k) =>k);
    const randomArr = arr.filter(item=>item != this.trackIndex);
    this.trackIndex = randomArr[Math.round(Math.random() * (randomArr.length-1))]; 
    return this.trackIndex;
  }
  initNote(now){
    if(this.startTime === null){
      this.startTime = now;
    }
    for(let i = 0; i < this.size; i++){
      let time = this.startTime + (this.duration / this.size) * (i+1);

      //myz 控制初始速度
      if(i/this.size<0.1){
        time += (this.duration / this.size) *7;
      }else if(i/this.size<0.2){  
        time += (this.duration / this.size) *(6.1);
      }else if(i/this.size<0.3){  
        time += (this.duration / this.size) *(5.2);
      }else if(i/this.size<0.4){  
        time += (this.duration / this.size) *(4.3);
      }else if(i/this.size<0.5){  
        time += (this.duration / this.size) *(3.4);
      }else if(i/this.size<0.6){  
        time += (this.duration / this.size) *(2.5);
      }else if(i/this.size<0.7){  
        time += (this.duration / this.size) *(1.6);
      }else{
        time += (this.duration / this.size) * (Math.random()-0.5);
      }
      // // 下面这个版本更随机
      // if(i/this.size<0.1){
      //   time += (this.duration / this.size) *7;
      // }else if(i/this.size<0.2){  
      //   time += (this.duration / this.size) *(6+Math.random());
      // }else if(i/this.size<0.3){  
      //   time += (this.duration / this.size) *(5+Math.random());
      // }else if(i/this.size<0.4){  
      //   time += (this.duration / this.size) *(4+Math.random());
      // }else if(i/this.size<0.5){  
      //   time += (this.duration / this.size) *(3+Math.random());
      // }else if(i/this.size<0.6){  
      //   time += (this.duration / this.size) *(2+Math.random());
      // }else if(i/this.size<0.7){  
      //   time += (this.duration / this.size) *(1+Math.random());
      // }else{
      //   time += (this.duration / this.size) * (Math.random()-0.5);
      // }

      const note = new Note({beatTime: time, width: this.width, height: this.height, beatHeight: this.beatHeight, onMiss: this.onMiss, trackIndex: this.getTrackIndex(), index: i, trackNum: this.trackNum});
      this.sprite.addChild(note.sprite);
      this.list.push(note);
    }
    //console.log(this.list)
  }
  play(){  
    this.initNote(new Date().valueOf());
  }
  stop(){
  }
  dance(now){
    const len = this.list.length;
    const playIndex =  this.playIndex;
    const list = this.list;

    this.playList = this.playList.filter(note=>note.status != 'end');

    for(let i = playIndex; i < len; i++){
      let note = list[i];
      if(now + 1000 >= note.beatTime){
        this.playIndex += 1;
        this.playList.push(note);
      }else{
        break;
      }
    }

    if(this.playList.length == 0){
        //this.onEnd && this.onEnd(); //myz 用时间控制结束，这里暂时不需要了
        return;
    }
    //console.log('---dance---',this.list.length,this.playList.length,this.playIndex,now)
    this.playList.forEach(note=>{
      note.fly(now);
    })

  }
}

